
package javaapplication1;

public class Bus {
    
    static Bus ob;
    String name;
    
    public Bus(String name){
        this.name = name;
    }
    
    public void print(){
        System.out.println(name);
    }
    
    @Override
    protected void finalize(){
        Bus.ob = this;
        System.out.println("Inside finalize : ");
        print();
    }
    
    public static void main(String[] args) throws InterruptedException {
        Bus b1 = new Bus("siddhartha's bus");
        
        Bus b2 = new Bus("ankit's bus");
        
        b1 = null;
        
        System.gc();
        
        Thread.sleep(3000);
        
        System.out.println("Inside main");
        Bus.ob.print();
        
        Bus.ob = null;
        System.out.println("Out of reference");
        System.gc();
        Thread.sleep(10000);
    }
    
}
